package com.chaokang.controller;

import com.chaokang.entity.Questionnaire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class QuestionnaireConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("http://chaoKang-questionnaire")
    private String url;

    @PostMapping(value = "/findById")
    public Questionnaire findById(@RequestBody Questionnaire questionnaire){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", questionnaire.getId());

        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Questionnaire a = restTemplate.postForObject(url + "/service/findById", objectHttpEntity, Questionnaire.class);
        return a;
    }

    @PostMapping(value = "/findByUserId")
    public List<Questionnaire> findByUserId(@RequestBody Questionnaire questionnaire){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", questionnaire.getUserId());

        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        List<Questionnaire> a = restTemplate.postForObject(url + "/service/findByUserId", objectHttpEntity, List.class);
        return a;
    }

    @PostMapping(value = "/insertQuestionnaire")
    public int insertQuestionnaire(@RequestBody Questionnaire questionnaire){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", questionnaire.getUserId());
        map.put("question", questionnaire.getQuestion());
        map.put("content", questionnaire.getContent());
        map.put("title", questionnaire.getTitle());
        map.put("questionNumber", questionnaire.getQuestionNumber());
        map.put("state", questionnaire.getState());
        map.put("answerNumber", questionnaire.getAnswerNumber());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Integer a = restTemplate.postForObject(url + "/service/insertQuestionnaire", objectHttpEntity, Integer.class);
        return a;
    }

    @PostMapping(value = "/findAll")
    public List<Questionnaire> findAll(@RequestBody Questionnaire questionnaire){
        HashMap<String, Object> map = new HashMap<>();
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        List<Questionnaire> a = restTemplate.postForObject(url + "/service/findAll", objectHttpEntity, List.class);
        return a;
    }

    @PostMapping(value = "/updateQuestionnaire")
    public int updateQuestionnaire(@RequestBody Questionnaire questionnaire){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", questionnaire.getId());
        map.put("userId", questionnaire.getUserId());
        map.put("question", questionnaire.getQuestion());
        map.put("content", questionnaire.getContent());
        map.put("title", questionnaire.getTitle());
        map.put("questionNumber", questionnaire.getQuestionNumber());
        map.put("state", questionnaire.getState());
        map.put("answerNumber", questionnaire.getAnswerNumber());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Integer a = restTemplate.postForObject(url + "/service/updateQuestionnaire", objectHttpEntity, Integer.class);
        return a;
    }

    @PostMapping(value = "/deleteQuestionnaire")
    public int deleteQuestionnaire(@RequestBody Questionnaire questionnaire){
        System.out.println(questionnaire.getId()+"------------------------------------");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", questionnaire.getId());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Integer a = restTemplate.postForObject(url + "/service/deleteQuestionnaire", objectHttpEntity, Integer.class);
        return a;
    }

    @PostMapping(value = "/recycleQuestionnaire")
    public int recycleQuestionnaire(@RequestBody Questionnaire questionnaire){
        System.out.println(questionnaire.getId()+"------------------------------------");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", questionnaire.getId());
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map);
        Integer a = restTemplate.postForObject(url + "/service/recycleQuestionnaire", objectHttpEntity, Integer.class);
        return a;
    }
}
